﻿/******************************************************************************/
#include "stdafx.h"
#include "Main.h"
/******************************************************************************/
// MANAGE
/******************************************************************************/
Item::Item()
{
   type=ITEM_NONE;
   type2=0;
   scale=0;
   power=0;
}
void Item::create(NetItem &src)
{
   NetItem &dest=T; dest=src;
   if(obj)
   {
      // name
      if(Param *p=obj->findParam("name"))name=p->asText();

      // type
      if(Param *p=obj->findParam("type" ))type =p->asEnum(ITEM_NONE);
      if(Param *p=obj->findParam("type2"))type2=p->asEnum();

      // power
      if(Param *p=obj->findParam("power"))power=p->asFlt();

      scale=obj->scale();
      mesh =obj->mesh ();

      // get icon
      for(Game::ObjParamsPtr op=obj; op; op=op->base()) // iterate through all ObjParams, to find first with a valid icon
         if(CChar *op_file_name=Game::Objs.name(op)) // if current ObjParams is stored in a file (file name is not NULL)
            if(icon.get(GetExtNot(op_file_name)+".ico.gfx")) // if there exists an icon with the same name as ObjParams but with different extension
               break; // break
   }
}
/******************************************************************************/
// GET
/******************************************************************************/
Bool Item::canBePutTo(SLOT_TYPE inv_slot)
{
   if(inv_slot==SLOT_TEMP)return true;

   switch(inv_slot)
   {
      case SLOT_HEAD : return type==ITEM_ARMOR && type2==ARMOR_HELMET  ;
      case SLOT_BODY : return type==ITEM_ARMOR && type2==ARMOR_BODY    ;
      case SLOT_HANDS: return type==ITEM_ARMOR && type2==ARMOR_GAUNTLET;
      case SLOT_LEGS : return type==ITEM_ARMOR && type2==ARMOR_PANTS   ;
      case SLOT_FEET : return type==ITEM_ARMOR && type2==ARMOR_BOOT    ;

      case SLOT_ARM_L:
      case SLOT_ARM_R:
      {
         if(type==ITEM_WEAPON)return true;
         if(type==ITEM_MISC && Contains(name, "Torch"))return true;
      }break;
   }

   return false;
}
void Item::matrix(Matrix &matrix)
{
   T._matrix_scaled=T._matrix=matrix;
   T._matrix_scaled.scaleOrn(scale);
}
/******************************************************************************/
// DRAW
/******************************************************************************/
void Item::drawIcon(C Vec2 &pos)
{
   if(icon)icon->drawFit(Rect_LU(pos.x, pos.y, icon->x()*PIXEL_SIZE, icon->y()*PIXEL_SIZE));
}
UInt Item::drawPrepare()
{
   if(mesh && Frustum(mesh->box, matrixScaled()))mesh->draw(matrixScaled(), vel, ang_vel);
   return 0;
}
void Item::drawShadow()
{
   if(mesh && Frustum(mesh->box, matrixScaled()))mesh->drawShadow(matrixScaled());
}
/******************************************************************************/
// IO
/******************************************************************************/
void Item::decompress(File &f)
{
   super::decompress(f);
   create(T);
}
/******************************************************************************/
